#include "stm32f10x.h"
#include "stdio.h"
 void LED_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void LED_G(uint8_t n)
{
	if(n)
				GPIO_SetBits(GPIOC, GPIO_Pin_13);
	else
				GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
void NVIC_Configuration()
{
	NVIC_InitTypeDef NVIC_InitStruct;	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	
	NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
 void USART_Config(void)  
{  
  GPIO_InitTypeDef x;
	USART_InitTypeDef y;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	x.GPIO_Pin=GPIO_Pin_9;
	x.GPIO_Mode=GPIO_Mode_AF_PP;
	x.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&x);
	x.GPIO_Pin=GPIO_Pin_10;
	x.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA,&x);
	y.USART_BaudRate=9600;
	y.USART_WordLength=USART_WordLength_8b;
	y.USART_StopBits=USART_StopBits_1;
	y.USART_Parity=USART_Parity_No;
	y.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	y.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	USART_Init(USART1,&y);
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	USART_Cmd(USART1,ENABLE);
	NVIC_Configuration();    
} 
int n=0;
void USART1_IRQHandler(void)  
{  
  u8 res;  
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)  
	{ 
		n++;
		res= USART_ReceiveData(USART1);   
		USART_SendData(USART1,res);
		if(res=='1')
		{
			LED_G(0);
		}
		else
		{
			LED_G(1);
		}		
  }  
}
int fputc(int ch,FILE *f)
{
	USART_SendData(USART1,(uint8_t)ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	return (ch);
}
int fgetc(FILE *f)
{
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	return (int)USART_ReceiveData(USART1);
}
uint8_t ch;
int main(void)  
{  
	USART_Config();
	LED_GPIO_Config(); 
  while(1);
} 
